<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
echo "<response>";
session_start();
if ($_SESSION['kteam_auth'] != true)
{
    $expire=time()+60*60;
    setcookie("redirector", "film.html", $expire);
    echo "<result>Login</result>";
    echo "</response>";
    //header("Location: login.html");
    exit;
}

include("sqlconnect.php");
include("sqlgenerator.php");
$debug = false;

if ($debug) {
    echo '<debug>';
    echo 'Post parameters: ' . $_POST['film_name'];
    echo '</debug>';
}

$fields[0] = $_GET['film_name'];
$fields[1] = $_GET['film_code'];
$fields[2] = $_GET['total_no_tickets'];
$fields[3] = $_GET['sold_no_tickets'];
$producer_code = $_GET['producer_code'];

db_connect();

// Query the producer ID from the producer code
$query = get_producer_id_from_code($producer_code);
if ($debug) {
    echo '<debug>';
    echo 'Query: ' . $query;
    echo '</debug>';
}
$result = db_query($query);
if ($debug) {
    echo '<debug>';
    echo 'Query Result: ' . $result;
    echo '</debug>';
}

$row = mysql_fetch_array($result);
try {
    $fields[4] = $row['id'];
} catch (Exception $e) {
    $fields[4] = 0;
}

// Check if the the film is already in the database
$query = get_film($fields[1]);
if ($debug) {
    echo '<debug>';
    echo 'Query: ' . $query;
    echo '</debug>';
}

$result = db_query($query);

if ($debug) {
    echo '<debug>';
    echo 'Query Result: ' .$resut;
    echo '</debug>';
}

if(mysql_num_rows($result) != 0) {
    // Film exists already so update the same
    $row = mysql_fetch_array($result);
    $query = update_film($row['id'], $fields);
    if ($debug) {
        echo '<debug>';
        echo 'Query: ' . $query;
        echo '</debug>';
    }

    $result = db_query($query);

    if ($debug) {
        echo '<debug>';
        echo 'Query Result: ' .$resut;
        echo '</debug>';
    }
    echo "<result>Updated</result>";
} else {
    // Add the film here
    $query = insert_film($fields);
    if ($debug) {
        echo '<debug>';
        echo 'Query: ' . $query;
        echo '</debug>';
    }

    $result = db_query($query);

    if ($debug) {
        echo '<debug>';
        echo 'Query Result: ' .$resut;
        echo '</debug>';
    }
    echo "<result>Added</result>";
}

echo "</response>";

?>